﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using News.Models;
using News.Models.Database;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace News.Controllers
{
	[Route("/[action].html")]
	[Route("[controller]/[action]")]
	public class HomeController : Controller
    {
		private readonly NewsContext _db;
		public HomeController(NewsContext db)
		{
			_db = db;
		}

        [Route("/")]
        public IActionResult Index(int id=0, string tt = "")
        {
            var nb = _db.News.ToList();
            ViewData["Index"] = nb;
            //分类
            if (id != 0)
            {
                ViewData["Index"] = _db.News.Where(x => x.Id == id).ToList();
            }
            //搜索
            if(tt !="")
            {
                ViewData["Index"] = _db.News.Where(x => x.NewsTitle.Contains(tt)).ToList();
            }

			return View();
        }

        public IActionResult Detail()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

    }
}
